import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import BookAdd from "../views/book/bookadd.vue"
import BookInfo from "../views/book/bookInfo.vue"
import BookUp from "../views/book/bookup.vue"
import UserAdd from "../components/logon.vue"
import userInfo from "../views/user/userinfo.vue"
import Login from "../components/login.vue"
import Upuser from "../components/userUpdata.vue"
Vue.use(VueRouter)

const routes = [

{
  path:"/",
  name:"home",
  component:Home,
  redirect:"/user"
},{
  path:"/login",
  component:Login

}
  ,{
    path: '/user',
    name: 'user',
    as:"/user",
    component: Home,
    meta:{
      auth:true
    },
    redirect:'/user/userInfo',
    children:[{
      path:'userInfo',
      component:userInfo,
      name:"userInfo",
      meta:{
        auth:true
      }
    },{
      path:"/user/userAdd",
      component:UserAdd,
      name:"userAdd",
      meta:{
        auth:true
      }
    },{
      path:"/user/upUser",
      component:Upuser,
      name:"upUser",
      meta:{
        auth:true
      }
    }]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'),
    redirect:'/about/bookInfo',
    meta:{
      auth:true
    },
    children:[{
      path:'bookInfo',
      component:BookInfo,
      name:"bookInfo",
      meta:{
        auth:true
      }
    },{
      path:"bookAdd",
      component:BookAdd,
      name:"bookAdd",
      meta:{
        auth:true
      }
    },{
      path:"bookUp",
      name:"bookUp",
      component:BookUp,
      meta:{
        auth:true
      }
    }
    ]
  }
]

const router = new VueRouter({
  routes
})


router.beforeEach((to,from,next)=>{
  if(to.meta.auth){//路由守卫，看是否需要 !$Cookies.get("adminId")
    if(!$Cookies.get("adminId")){
      next('/login');
    }else{
      next();
    }
  }else{
    next();
  }
})




export default router
